package cn.java.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author 小石潭记
 * @date 2021/7/18 10:53
 * @Description: ${todo}
 */
public class Test {
    public static final List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, EmpolyeeStatus.BUSY),
            new Employee("李四", 38, 8888.88, EmpolyeeStatus.FREE),
            new Employee("王五", 50, 6666.66, EmpolyeeStatus.BUSY),
            new Employee("赵六", 16, 5555.55, EmpolyeeStatus.FREE),
            new Employee("田七", 20, 3333.33, EmpolyeeStatus.VACATE)
    );

    public static void main(String[] args) {
        employees.stream().forEach(item -> {
            System.out.println(item);
        });

        employees.stream().map(Employee::getName).forEach(System.out::println);

        employees.stream().filter(e -> e.getSalary() > 5000)
                .forEach(System.out::println);

        // 创建stream
        // 1.通过Collection系列集合提供的stream()或者parallelStream()
        List<String> list = new ArrayList<>();
        Stream<String> stream1 = list.stream();

        // 2.通过Arrays中的静态方法stream()获取数组流
        Employee[] emps = new Employee[10];
        Stream<Employee> stream2 = Arrays.stream(emps);

        //3.通过stream中的静态方法of()
        Stream<String> stream3 = Stream.of("11", "22", "33");

        //4.创建无限流
        Stream<Integer> stream4 = Stream.iterate(0, x -> x + 2);

//        employees.stream().map();
//        employees.stream().flatMap();
//        他们的区别就是list.add("123", {"1","2","3"});
//        list.addAll("123", "1","2","3");
        /**
         * 映射
         * map 接收lambda 将元素转换成其他形式或提取信息。接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素。
         * flatMap 接收一个函数作为参数，将流中的每个值都转换成另一个流，然后把所有流连接成一个流
         */
        List<String> list1 = Arrays.asList("ccc", "bbb", "ddd", "aaa");
        Stream<Stream<Character>> streamStream = list1.stream().map(Test::filterCharacter);
        streamStream.forEach(sm -> sm.forEach(System.out::println)); // { {a,a,a}, {b,b,b}}
        System.out.println("----------------------------");
        Stream<Character> characterStream = list1.stream().flatMap(Test::filterCharacter);
        characterStream.forEach(System.out::println); //{ a,a,a,b,b,b}

    }

    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();
        for(Character ch : str.toCharArray()) {
            list.add(ch);
        }
        return list.stream();
    }

}
